#include "PolyFactory.h"
#define PI 3.141592
#define bevel 12

/*
 *	Poly constructor with indiv. points
 */

PolyFactory::PolyFactory(World *world)
{
	m_world = world;
}

/*
 *	Poly Destructor
 */

PolyFactory::~PolyFactory(void){}

void PolyFactory::createRectagle(Ogre::String name, Ogre::Vector2 position, Ogre::Real depth, Ogre::Real width, Ogre::Real height, Ogre::Real angle, bool boundingVisible, bool enhancedCollisionShape, int type)
{
	std::vector<Ogre::Vector2> polyVector;

	//nerate points
	polyVector.push_back(Ogre::Vector2(position.x - width/2,(position.y + height/2) - bevel));
	polyVector.push_back(Ogre::Vector2((position.x - width/2) + bevel,position.y + height/2));

	polyVector.push_back(Ogre::Vector2((position.x + width/2) - bevel,position.y + height/2));
	polyVector.push_back(Ogre::Vector2(position.x + width/2,(position.y + height/2) - bevel));

	polyVector.push_back(Ogre::Vector2(position.x + width/2,(position.y - height/2) + bevel));
	polyVector.push_back(Ogre::Vector2((position.x + width/2) - bevel,position.y - height/2));

	polyVector.push_back(Ogre::Vector2((position.x - width/2) + bevel,position.y - height/2));
	polyVector.push_back(Ogre::Vector2(position.x - width/2,(position.y - height/2) + bevel));

	polyVector.push_back(Ogre::Vector2(position.x - width/2,(position.y + height/2) - bevel));

	//apply rotation
	if(angle != 0)
	{
		Ogre::Real angleRAD = angle * PI / 180;

		for(int i = 0; i < polyVector.size(); i ++)
		{
			Ogre::Vector2 point = polyVector.at(i);
			polyVector.at(i).x = position.x + (cos(angleRAD) * (point.x - position.x) - sin(angleRAD) * (point.y - position.y)); 
			polyVector.at(i).y = position.y + (sin(angleRAD) * (point.x - position.x) + cos(angleRAD) * (point.y - position.y)); 
		}
	}

	Poly *rectangle = new Poly(name, position, depth, angle, boundingVisible, type, polyVector, m_world->m_sceneManager);
	m_world->addPolygonToWorld(rectangle);
}

void PolyFactory::createTriangle(Ogre::String name, Ogre::Vector2 p1, Ogre::Vector2 p2, Ogre::Vector2 p3, Ogre::Real angle, bool showBounding)
{
	std::vector<Ogre::Vector2> polyVector;
	polyVector.push_back(p1);
	polyVector.push_back(p2);
	polyVector.push_back(p3);
	polyVector.push_back(p1);

	//Poly *triangle = new Poly(name,  showBounding, STATIC, polyVector, m_world->m_sceneManager);
	//m_world->addPolygonToWorld(triangle);
}

void PolyFactory::createCube(Ogre::String name, Ogre::Vector2 p1, Ogre::Vector2 p2, Ogre::Vector2 p3, Ogre::Vector2 p4, Ogre::Real angle, bool showBounding)
{
	std::vector<Ogre::Vector2> polyVector;
	polyVector.push_back(p1);
	polyVector.push_back(p2);
	polyVector.push_back(p3);
	polyVector.push_back(p4);
	polyVector.push_back(p1);

	//Poly *cube = new Poly(name, showBounding, STATIC, polyVector, m_world->m_sceneManager);
	//m_world->addPolygonToWorld(cube);
}

void PolyFactory::createRoundedCube(Ogre::String name, Ogre::Vector2 p1, Ogre::Vector2 p2, Ogre::Vector2 p3, Ogre::Vector2 p4, Ogre::Real angle, bool showBounding)
{
	std::vector<Ogre::Vector2> polyVector;
	polyVector.push_back(Ogre::Vector2(p1.x,p1.y-10));
	polyVector.push_back(Ogre::Vector2(p1.x+10,p1.y));
	polyVector.push_back(Ogre::Vector2(p2.x-10,p2.y));
	polyVector.push_back(Ogre::Vector2(p2.x,p2.y-10));
	polyVector.push_back(Ogre::Vector2(p3.x,p3.y+10));
	polyVector.push_back(Ogre::Vector2(p3.x-10,p3.y));
	polyVector.push_back(Ogre::Vector2(p4.x+10,p4.y));
	polyVector.push_back(Ogre::Vector2(p4.x,p4.y+10));
	polyVector.push_back(Ogre::Vector2(p1.x,p1.y-10));

	//Poly *rCube = new Poly(name, showBounding, STATIC, polyVector, m_world->m_sceneManager);
	//m_world->addPolygonToWorld(rCube);
}